package com.atguigu.mybatisplus.controller;

import com.atguigu.mybatisplus.bean.Employee;
import com.atguigu.mybatisplus.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created by Smexy on 2023/3/28
 */
@RestController
public class EmployeeController
{

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping(value = "/emp")
    public Object handle1(String op,Integer id,String lastname,String gender,String email ){

        //封装数据模型
        Employee employee = new Employee(id, lastname, gender, email);

        switch (op){
            case "select":  if (id == null){
                return "id非法!";
            }else {
                Employee e = employeeService.getById(id);
                return e == null ? "查无此人" : e;
            }

            case "delete":if (id == null){
                return "id非法!";
            }else {
                employeeService.removeById(id);
                return "ok";
            }

            case "insert": employeeService.save(employee);
                return "ok";

            case "update": if (id == null){
                return "id非法!";
            }else {
                employeeService.updateById(employee);
                return "ok";
            }

            default: return "ok";
        }

    }

    @RequestMapping(value = "/getAllEmp")
    public Object handle2(){
        List<Employee> all = employeeService.list();
        return all;
    }


}
